package com.xhsj.user.cram.web;

import com.xhsj.user.cram.dto.BaseParamDto;
import com.xhsj.user.cram.dto.ComputerRoomAccessApplyExamineDto;
import com.xhsj.user.cram.dto.ComputerRoomAccessApplyExamineTwoDto;
import com.xhsj.user.cram.dto.ComputerRoomAccessApplySaveDto;
import com.xhsj.user.cram.entity.TbComputerRoomAccessApply;
import com.xhsj.user.cram.service.TbComputerRoomAccessApplyService;
import com.xhsj.user.cram.vo.FindComputerRoomAccessDetailedVo;
import com.xhsj.user.cram.vo.FindComputerRoomAccessHiTaskVo;
import com.xhsj.user.cram.vo.FindComputerRoomAccessRunTaskVo;
import com.xhsj.user.generalprocess.vo.GeneralAuditHistoryVo;
import com.xhsj.user.hr.base.BaseParam;
import com.xhsj.user.pms.patent.vo.ProcessNodeVo;
import com.xhsj.user.pms.patent.vo.FindHiTaskVo;
import com.xhsj.user.utils.Message;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;


/**
 * <p>
 * 机房出入申请 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Apr 19, 2021 7:26:32 AM
 */
@Slf4j
@Api(tags = "机房出入申请")
@RestController
@RequestMapping("/cram/tbComputerRoomAccessApply")
public class TbComputerRoomAccessApplyController {

	@Autowired
    public TbComputerRoomAccessApplyService service;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Apr 19, 2021 7:26:32 AM
	 */
	@ApiOperation(value = "机房出入申请,保存/修改", notes = "机房出入申请,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:computer:room:access:apply:edit')")
	public Message save(@RequestBody TbComputerRoomAccessApply data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Apr 19, 2021 7:26:32 AM
	 */
	@ApiOperation(value = "机房出入申请删除", notes = "通过id删除机房出入申请信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:computer:room:access:apply:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author liuqinghua
	 * @date Apr 19, 2021 7:26:32 AM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:computer:room:access:apply:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Apr 19, 2021 7:26:32 AM
    */
	@ApiOperation(value = "分页查询机房出入申请list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:computer:room:access:apply:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbComputerRoomAccessApply> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Apr 19, 2021 7:26:32 AM
    */
	@ApiOperation(value = "查询机房出入申请所有数据list", notes = "查询机房出入申请所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:computer:room:access:apply:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Apr 19, 2021 7:26:32 AM
    */
	@ApiOperation(value = "通过id获取机房出入申请实体类", notes = "通过id获取机房出入申请实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author liuqinghua
    * @date Apr 19, 2021 7:26:32 AM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
	public Message detail(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

	   /**
	       * @Description: 待审核数据
	       * @Param:  com.xhsj.user.hr.base.data
	       * @Author: liuqinghua
	       * @Date: 2021/4/19
	       */
	   @ApiOperation(value = "机房出入-待审核数据", notes = "机房出入-待审核数据")
	   @PostMapping(value="/findComputerRoomAccessRunTask")
		public Message findComputerRoomAccessRunTask(@RequestBody BaseParamDto data){
		   try {
		   log.info("机房出入-待审核数据参数{}", data);
               IPage<FindComputerRoomAccessRunTaskVo> computerRoomAccessRunTask = service.findComputerRoomAccessRunTask(data);
               return Message.success("成功",computerRoomAccessRunTask.getRecords(),computerRoomAccessRunTask.getTotal(),computerRoomAccessRunTask.getPages());
		   }catch (Exception e){
			   e.printStackTrace();
			   return Message.error(1,e.getMessage());
		   }
		}

	/**
	 * 查询已审核数据
	 * @author 苏小林
	 * @date 2021/4/20 14:01
	 * @param: baseParam
	 * @return: com.xhsj.user.utils.Message
	 */
	@RequestMapping(value = "/findComputerRoomAccessHiTask", method = RequestMethod.POST)
	public Message findComputerRoomAccessHiTask(@RequestBody BaseParam baseParam) {

		try {
			IPage<FindComputerRoomAccessHiTaskVo> selectPage = service.findComputerRoomAccessHiTask(baseParam);
			return Message.success("成功", selectPage.getRecords(), selectPage.getTotal(), selectPage.getPages());
		} catch (Exception e) {
			log.error("失败:{}", e);
			return Message.error(1, e.getMessage());
		}
	}

	/**
	 * @Description: 我的申请数据
	 * @Param:  com.xhsj.user.hr.base.data
	 * @Author: liuqinghua
	 * @Date: 2021/4/20
	 */
	@ApiOperation(value = "机房出入-我的申请", notes = "机房出入-我的申请")
	@PostMapping(value="/findMyComputerRoomAccessApply")
	public Message findMyComputerRoomAccessApply(@RequestBody BaseParamDto data){
		try {
			log.info("机房出入-我的申请参数{}", data);
			IPage<FindComputerRoomAccessRunTaskVo> findMyComputerRoomAccessApply = service.findMyComputerRoomAccessApply(data);
			return Message.success("成功",findMyComputerRoomAccessApply.getRecords(),findMyComputerRoomAccessApply.getTotal(),findMyComputerRoomAccessApply.getPages());
		}catch (Exception e){
			e.printStackTrace();
			return Message.error(1,e.getMessage());
		}
	}

	/**
	 * @Description: 审批页面展示数据
	 * @Param:  com.xhsj.user.hr.base.data
	 * @Author: liuqinghua
	 * @Date: 2021/4/20
	 */
	@ApiOperation(value = "机房出入-审批页面展示", notes = "机房出入-审批页面展示")
	@GetMapping(value="/findComputerRoomAccessDetailed")
	public Message findComputerRoomAccessDetailed(@RequestParam Long dataId){
		try {
			log.info("机房出入-审批页面展示参数{}", dataId);
			FindComputerRoomAccessDetailedVo findComputerRoomAccessDetailed = service.findComputerRoomAccessDetailed(dataId);
			return Message.success("成功",findComputerRoomAccessDetailed);
		}catch (Exception e){
			e.printStackTrace();
			return Message.error(1,e.getMessage());
		}
	}

	   /**
	       * @Description:  机房出入-部门提交需求保存
	       * @Param: com.xhsj.user.cram.dto.ComputerRoomAccessApplyStartDto
	       * @Author: liuqinghua
	       * @Date: 2021/4/19
	       */
	   @ApiOperation(value = "机房出入-部门提交需求保存", notes = "机房出入-部门提交需求保存")
	   @PostMapping(value="/computerRoomAccessDeptApplySave")
	   public Message computerRoomAccessDeptApplySave(@RequestBody ComputerRoomAccessApplySaveDto data, BindingResult bindingResult) {
	   	try {
			log.info("机房出入-部门提交需求保存参数{}", data);
			if (bindingResult.hasErrors()) {
				log.info(bindingResult.getFieldError().getDefaultMessage());
				return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
			}
			service.computerRoomAccessDeptApplySave(data);
			return Message.success("成功");
		}catch (Exception e){
	   		e.printStackTrace();
			return Message.error(1,e.getMessage());
		}
	   }

    /**
     * @Description:  机房出入流程-网络信息安全工程师处理
     * @Param: com.xhsj.user.cram.dto.ComputerRoomAccessApplySaveDto
     * @Author: liuqinghua
     * @Date: 2021/4/19
     */
    @ApiOperation(value = "机房出入流程-网络信息安全工程师处理", notes = "机房出入流程-网络信息安全工程师处理")
    @PostMapping(value="/computerRoomAccessApplyExamine_2")
    public Message computerRoomAccessApplyExamine_2(@RequestBody ComputerRoomAccessApplyExamineDto data, BindingResult bindingResult) {
        try {
            log.info("机房出入流程-网络信息安全工程师处理参数{}", data);
            if (bindingResult.hasErrors()) {
                log.info(bindingResult.getFieldError().getDefaultMessage());
                return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
            }
            service.computerRoomAccessApplyExamine_2(data);
            return Message.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Message.error(1,e.getMessage());
        }
    }

       /**
           * @Description:综合管理部负责人审批
           * @Param:com.xhsj.user.cram.dto.ComputerRoomAccessApplyExamineDto
           * @Author: liuqinghua
           * @Date: 2021/4/20
           */
	   @ApiOperation(value = "机房出入流程-综合管理部负责人审批", notes= "机房出入流程-综合管理部负责人审批")
	   @PostMapping(value="/computerRoomAccessApplyExamine_3")
	   public Message computerRoomAccessApplyExamine_3(@RequestBody ComputerRoomAccessApplyExamineDto data, BindingResult bindingResult) {
		   try {
			   log.info("机房出入流程-综合管理部负责人审批参数{}", data);
			   if (bindingResult.hasErrors()) {
				   log.info(bindingResult.getFieldError().getDefaultMessage());
				   return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
			   }
			   service.computerRoomAccessApplyExamine_3(data);
			   return Message.success("成功");
		   }catch (Exception e){
			   e.printStackTrace();
			   return Message.error(1,e.getMessage());
		   }
	   }

	      /**
	          * @Description:  流程列表
	          * @Param:  dataId业务id nodeId当前节点id
	          * @Author: liuqinghua
	          * @Date: 2021/4/20
	          */
		  @ApiOperation(value = "机房出入流程-流程列表", notes= "机房出入流程-流程列表")
		  @GetMapping(value="/findComputerRoomAccessProcessList")
		  public Message findComputerRoomAccessProcessList(@RequestParam(value = "dataId") String dataId,@RequestParam(value = "nodeId") Long nodeId) {
			  try {
				  Map<String,Object> map=new HashMap<>();
				  List<ProcessNodeVo> computerRoomAccessProcessList = service.findComputerRoomAccessProcessList(dataId);
				  map.put("nodeList",computerRoomAccessProcessList);
				  map.put("cutNodeId",nodeId==-1?1:nodeId);
				  return Message.success("成功",map);
			  }catch (Exception e){
				  e.printStackTrace();
				  return Message.error(1,e.getMessage());
			  }
		  }

	/**
	 * @Description:  流程历史列表
	 * @Param:BaseParam
	 * @Author: liuqinghua
	 * @Date: 2021/4/20
	 */
	@ApiOperation(value = "机房出入流程-流程历史列表", notes= "机房出入流程-流程历史列表")
	@GetMapping(value="/findComputerRoomAccessHiProcessList")
	public Message findComputerRoomAccessHiProcessList(@RequestParam("dataId") Long dataId,
													   @RequestParam(value = "page", defaultValue = "1") int page,
													   @RequestParam(value = "size", defaultValue = "20") int size) {
		try {
			IPage<GeneralAuditHistoryVo> selectPage = service.findComputerRoomAccessHiProcessList(dataId, page, size);
			return Message.success("成功", selectPage.getRecords(), selectPage.getTotal(), selectPage.getPages());
		} catch (Exception e) {
			log.error("失败:{}", e);
			return Message.error(1, e.getMessage());
		}
	}

	/**
	 * @Description:  出入记录列表
	 * @Param:BaseParam
	 * @Author: liuqinghua
	 * @Date: 2021/4/21
	 */
	@ApiOperation(value = "机房出入流程-出入记录列表", notes= "机房出入流程-出入记录列表")
	@PostMapping(value="/findEntryOutRecordList")
	public Message findEntryOutRecordList(@RequestBody BaseParamDto data) {
		try {
			IPage<FindComputerRoomAccessRunTaskVo> selectPage = service.findEntryOutRecordList(data);
			return Message.success("成功", selectPage.getRecords(), selectPage.getTotal(), selectPage.getPages());
		} catch (Exception e) {
			log.error("失败:{}", e);
			return Message.error(1, e.getMessage());
		}
	}
}

